--[[
注意正式版更改表格顺序
有交叉的记得放在最底下

]]
local data = load_excel('存档数据.xlsx','天赋页存档')
local keys = ac.register_dzscore3('TF',40,1)
local map = {}
local lock = {}     --前置激活连接

--[解析数据]
for id,info in ipairs(data) do
    local name = info.name or info['对应']
    local link = info.link and split_string(info.link,',') or {}
    info.id = id
    info.code = keys[id]
    info.link = link
    info.tip = format_obj(info,info['效果'])

    map[name] = info
    if info.code==nil then
        log.error('没有注册天赋树存档',id,name)
    end
    
    --[添加前置连接]
    for a,key in ipairs(link) do
        lock[key] = true
    end
end



ac.game:event '玩家-存档读取前'(function(_,player)
    local list = {}
    local allow = {}
    local sum = 0
    for id,info in ipairs(data) do
        local name = info.name or info['对应']
        local lv = player:dz_get(info.code)
        local link = info.link or {}
        if lv>0 and (lock[name]==nil or allow[name]) then
            list[id] = lv
            sum = sum + lv
            --[激活后续]
            for a,key in ipairs(link) do
                allow[key] = true
            end
        else
            list[id] = 0
        end
    end
    player:set_data('天赋树-总等级',sum)
    player:set_data('天赋树-节点等级',list)
    player:set_data('天赋树-节点允许',allow)
end)


ac.game:event '玩家-天赋树加点'(function(_,player,name,count,hide)
    local list = player:get_data('天赋树-节点等级') or {} --保存等级
    local allow = player:get_data('天赋树-节点允许') or {} --保存等级
    local sum = player:get_data('天赋树-总等级') or 0
    local info = map[name]
    local link = info.link or {}
    local count = count or 1
    local level = list[info.id] or 0
    local minlv = info['限制等级'] or 0
    
    if sum<minlv or (lock[name] and allow[name]==nil) then
        return
    end
    for a=1,count do
        --加点条件
        if level==info['等级'] then
            break
        end
        --[激活后续]
        for a,key in ipairs(link) do
            allow[key] = true
        end
        level = level + 1
        sum = sum + 1
        
        if player:is_self() then
            info.level = level
        end
    end
    player:set_data('天赋树-总等级',sum)
    player:dz_set(info.code,level)
    --保存数据
    list[info.id] = level
end)


ac.sync:event '同步-天赋树加点'(function(_,player,name,count)
    player:notify('玩家-天赋树加点',player,name,count)
end)
